(function ($) {
	$(function () {
		// 显示/隐藏购物车
		var
			oShopCar = $('#shop-car'),
			oShoCarInfo = $('#shop-car-info'),
			oShopCarList = $('#shop-car-list');

		oShopCar.mouseenter(function () {
			oShoCarInfo.addClass('hover');
			oShopCarList.css('display', 'block');
		}).mouseleave(function () {
			oShoCarInfo.removeClass('hover');
			oShopCarList.css('display', 'none');
		});

		// 搜索框
		var
			oSeachBox	  = $('#search-box'),
			oSeachContent = $('#search-content'),
			oSeachNotice  = $('#search-notice'),
			oSearchList   = $('#search-list'),
			iSearchIndex  = -1,
			sOriHtml 	  = oSearchList.html();
		oSeachContent.focus(function () {
			oSeachBox.addClass('search-box-hover');
			oSeachNotice.fadeOut('fast');
			oSearchList.css('display', 'block');
		}).blur(function () {
			oSeachBox.removeClass('search-box-hover');
			oSeachNotice.fadeIn('fast');
			oSearchList.css('display', 'none');
		}).keyup(function (ev) {
			var
				ev = ev || window.event,
				aLi = $('#search-list li');
			if(ev.keyCode === 38 || ev.keyCode === 40) {
				if(ev.keyCode === 38 && iSearchIndex > 0) {
					iSearchIndex--;
				} else if(ev.keyCode === 40 && iSearchIndex < aLi.length - 1) {
					iSearchIndex++;
				}
				oSeachContent.val(aLi.removeClass('active').eq(iSearchIndex).addClass('active').data('key'));
			}
		}).bind('input propertychange', function () {
			var sSearchCon = $(this).val();
			if(sSearchCon) {
				$.ajax({
					url: 'http://search.mi.com/search/expand',
					data: {keyword: sSearchCon},
					jsonp: 'jsonpcallback',
					//jsonpCallback: 'xmsearch',
					dataType: 'jsonp',
					success: function (data) {
						var sHtml = '';
						data.forEach(function (v) {
							sHtml += '<li data-key="' + v.Key + '"><a href="javascript:;"><span>约有' + v.Rst + '件</span>' + v.Key + '</a></li>';
						});
						if(sHtml) {
							oSearchList.css('display', 'block').html(sHtml);
						} else {
							oSearchList.css('display', 'none');
						}
						iSearchIndex = -1;
					}
				});
			} else {
				oSearchList.html(sOriHtml);
				iSearchIndex = -1;
			}
		});

		// 显示主菜单栏
		var
			oNavTime = null, 
			oShowNav = $('#nav-menu-wrap');
		$('#show-nav').mouseenter(function () {
			clearInterval(oNavTime);
			oShowNav.css({display:'block'}).stop().animate({
							height: 234,
						}, 500);
		}).mouseleave(function () {
			oNavTime = setTimeout( function () {
				oShowNav.stop().animate({
					height:0
				}, 500, function () {
					oShowNav.css('display', 'none');
				});
			}, 50);
		});
		$('#show-nav a').mouseover(function () {
			var iIndex = $(this).index();
			$('#nav-menu ul').css('display', 'none').eq(iIndex).css('display', 'block');
		});

		oShowNav.mouseenter(function () {
			clearInterval(oNavTime);
		}).mouseleave(function () {
			oNavTime = setTimeout( function () {
				oShowNav.stop().animate({
					height:0
				}, 500, function () {
					oShowNav.css('display', 'none');
				});
			}, 50);
		});

		// 轮播图
		var
			aBtnA = $('#btn-list a'),
			aBannerImg = $('#image-list img'),
			iIndex = 0,
			oBannerTime = null;

		// 点击按钮
		aBtnA.click(function () {
			iIndex = $(this).index();
			bannerMove(iIndex);
		});

		// 往左运动
		$('.left-btn').click(function() {
			iIndex--;
			if(iIndex < 0) {
				iIndex = aBannerImg.length - 1;
			}
			bannerMove(iIndex);
		});

		// 往右运动
		$('.right-btn').click(function() {
			iIndex++;
			if(iIndex >= aBannerImg.length) {
				iIndex = 0;
			}
			bannerMove(iIndex);
		});

		// 清除定时器
		$('#banner').hover(function () {
			clearInterval(oBannerTime);
		}, function () {
			autoMove();
		});

		// 自动轮播
		autoMove();

		function autoMove() {
			oBannerTime = setInterval(function () {
				iIndex++;
				if(iIndex >= aBannerImg.length) {
					iIndex = 0;
				}
				bannerMove(iIndex);
			}, 3000);
		}

		function bannerMove(iIndex){
			aBtnA.removeClass('active').eq(iIndex).addClass('active');
			aBannerImg.stop(true).animate({opacity:0}, 500, function () {
				$(this).css('display', 'none');
			}).eq(iIndex).css('display', 'block').stop(true).animate({opacity:1}, 500);
		} 
	});
})(jQuery);